﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Lavka.Models;

namespace Lavka.Core
{
    public enum SortFilter
    {
        Name = 0,
        Category = 1,
        Brand = 2,
        Price = 3
    }
    public class ProductsHelper
    {
        public static string[] GetByString(string query, LavkaDbEntities context)
        {
            var references = (from r in context.Products
                              where (r.Reference.StartsWith(query) || r.Name.StartsWith(query))
                              orderby r.Reference // optional but it'll look nicer
                              select r.Reference +" | " + r.Name).Distinct().ToArray();
            return references;
        }

        public static string[] GetProductReferences(string query, LavkaDbEntities context)
        {
            var references = (from r in context.Products
                              where r.Reference.StartsWith(query)
                              orderby r.Reference // optional but it'll look nicer
                              select r.Reference).Distinct().ToArray();
            return references;
        }
        public static string[] GetProductNames(string query, LavkaDbEntities context)
        {
            var names = (from r in context.Products
                              where r.Name.Contains(query)
                              orderby r.Name // optional but it'll look nicer
                              select r.Name).Distinct().ToArray();
            return names;
        }
        public static List<Products> ViewedProducts
        {
            get
            {
                if (HttpContext.Current.Session["ViewedProducts"] == null) HttpContext.Current.Session["ViewedProducts"] = new List<Products>();
                return (List<Products>)HttpContext.Current.Session["ViewedProducts"];
            }
            set
            {
                HttpContext.Current.Session["ViewedProducts"] = value;
            }
        }

        public static List<Products> GetProducts(int categoryId, LavkaDbEntities context, SortFilter filter, bool acsending = true)
        {
            if (categoryId > 0)
                return Sort(context.Products.Where(p => p.CategoryId == categoryId), filter, acsending).Take(15).ToList();               
                return Sort(context.Products, filter, acsending).Take(15).ToList();               
        }
        public static List<Products> GetProducts(int categoryId, int pageIndex, LavkaDbEntities context, 
            SortFilter filter, bool acsending = true)
        {
            if (categoryId > 0)
                return
                    Sort(context.Products.Where(p => p.CategoryId == categoryId), filter, acsending).Skip(15*pageIndex).
                        Take(15).ToList();

                return Sort(context.Products, filter, acsending).Skip(15 * pageIndex).Take(15).ToList();
        }

        public static List<Products> Sort(IQueryable<Products> data, SortFilter filter, bool acsending)
        {
            if (acsending)
            {
                switch (filter)
                {
                    case SortFilter.Category:
                        return data.OrderBy(p => p.Caterories.Name).ToList();
                    case SortFilter.Brand:
                        return data.OrderBy(p => p.Brands.Name).ToList();
                    case SortFilter.Price:
                        return data.OrderBy(p => p.Price).ToList();
                    default:
                        return data.OrderBy(p => p.Name).ToList();
                }                  
            }
            else
            {
                switch (filter)
                {
                    case SortFilter.Category:
                        return data.OrderByDescending(p => p.Caterories.Name).ToList();
                    case SortFilter.Brand:
                        return data.OrderByDescending(p => p.Brands.Name).ToList();
                    case SortFilter.Price:
                        return data.OrderByDescending(p => p.Price).ToList();
                    default:
                        return data.OrderByDescending(p => p.Name).ToList();
                }                
            }
        }

        public static List<Articles> GetProductArticles(int productId, LavkaDbEntities context)
        {
            var product = context.Products.Where(c => c.ProductId == productId).FirstOrDefault();
            if (product != null) return product.Articles.ToList();
            return null;
        }

        public static bool IsProductInBase(string reference, LavkaDbEntities context)
        {
            var products = context.Products.Where(c => c.Reference == reference);
            if (products.Count() > 0) return true;
            return false;
        }

        public static Products GetProductByReference(string reference, LavkaDbEntities context)
        {
            var products = context.Products.Where(c => c.Reference == reference).FirstOrDefault();
            if (products != null) return products;
            return null;
        }

        public static string GetProductPrice(Products product, LavkaDbEntities context)
        {
            var actionPrice = ActionsHelper.GetActionPrice(product, context);

            if (actionPrice != null)
            {
                return string.Format("{0:0.00}", actionPrice);
            }

            return string.Format("{0:0.00}", product.Price);
        }
    }
}